import { loginByUsername, logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    },
    companyId: localStorage.getItem("companyId") ,

  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },


  },

  actions: {
    // 用户名登录
    async LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim()
      let { data } = await loginByUsername(username, userInfo.code, userInfo.company_id);
      const tokenMap = data.data

      commit('SET_TOKEN', tokenMap.token)
      commit('SET_NAME', tokenMap.user.user_name)
      commit('SET_AVATAR', tokenMap.user.portrait);
      localStorage.setItem("ossPublicUrl",tokenMap.oss_public_url);
      localStorage.setItem("companyId", tokenMap.user.company_id)
      localStorage.setItem("loginUser",JSON.stringify(tokenMap.user))
      localStorage.setItem('avatar',tokenMap.user.portrait)
      setToken({ key:tokenMap.token_key,val:tokenMap.token })
    },


    // 获取用户信息
    async GetUserInfo({ commit, state }) {
        let response = await getUserInfo(state.token);
        const data = response.data.data
        commit('SET_ROLES', data.roles)
        commit('SET_NAME', data.name)
        commit('SET_AVATAR', data.avatar)
        localStorage.setItem('avatar',data.avatar)
        commit('SET_INTRODUCTION', data.introduction)
        return response
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    async LogOut({ commit, state }) {
        let user = JSON.parse(localStorage.getItem("loginUser"));
        await logout(user);
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        // localStorage.clear();
        localStorage.removeItem("TokenKey");
        localStorage.removeItem("companyId");
        localStorage.removeItem("loginUser");
        localStorage.removeItem("ossPublicUrl");
    },

    // 前端 登出
    async FedLogOut({ commit }) {
      commit('SET_TOKEN', '')
      removeToken()
    },

    // 动态修改权限
    async ChangeRoles({ commit }, role) {
        commit('SET_TOKEN', role)
        setToken(role)
        let { data } = await getUserInfo(role)
        const result = data.data
        commit('SET_ROLES', result.roles)
        commit('SET_NAME', result.name)
        commit('SET_AVATAR', result.avatar)
        localStorage.setItem('avatar',result.avatar)
        commit('SET_INTRODUCTION', result.introduction)
    }
  }
}

export default user
